package com.sxfq.loansupermarket.backsystem.modular.sharesHolding.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.sharesHolding.entity.SearchSharesAreCompanyInfo;
import com.sxfq.loansupermarket.backsystem.modular.sharesHolding.entity.SharesAreCompanyInfo;
import com.sxfq.loansupermarket.backsystem.modular.sharesHolding.entity.SharesArePeopleInfo;
import com.sxfq.loansupermarket.backsystem.modular.sharesHolding.service.SharesAreCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 质量管理控制器
 *
 * @author fengshuonan
 * @Date 2019-09-01 15:34:05
 */
@Controller
@RequestMapping("/sharesAreCompany")
public class SharesAreCompanyController extends BaseController {
    @Autowired
    SharesAreCompanyService service;

    @Autowired
    private SqlMapper sqlMapper;


    private String PREFIX = "/sharesHolding/";

    /**
     * 跳转到量管理首页
     */
    @RequestMapping("")
    public String index(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "sharesAreCompany.html";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchSharesAreCompanyInfo info, HttpServletRequest request) {
        Page<SharesAreCompanyInfo> page = new PageFactory<SharesAreCompanyInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<SharesAreCompanyInfo> list = service.getList(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.getList(page, info);
            }
            page.setRecords(list);
            page.setTotal(service.getQueryPagesCount(page, info));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    //跳到添加页面
    @RequestMapping("/sharesAreCompanyAdd")
    public String sharesAreCompanyAdd() {
        return PREFIX + "sharesAreCompanyAdd.html";
    }

    //查询对外投资
    public List<SharesAreCompanyInfo> getOuterList() {
        //对外投资
        String sqlo="select a.id,a.company_name from share_sare_company_info a where a.company_type=1 ";
        List<SharesAreCompanyInfo> outerList=sqlMapper.selectList(sqlo,SharesAreCompanyInfo.class);
        return outerList;
    }

    //查询分支机构
    public List<SharesAreCompanyInfo> getBranchList() {
        //分支机构
        String sqlB="select a.id,a.company_name from share_sare_company_info a where a.company_type=2 ";
        List<SharesAreCompanyInfo> branchList=sqlMapper.selectList(sqlB,SharesAreCompanyInfo.class);
        return branchList;
    }

    //添加
    @RequestMapping(value = "/sharesAreCompanyAddOkLeft", method = RequestMethod.POST)
    @ResponseBody
    public List<SharesAreCompanyInfo> sharesAreCompanyAddOkLeft(String companyName, String companyType,String address,String money,String setupTime) {
        String sql2 = "INSERT INTO share_sare_company_info (company_name,address, createtime,money,setupTime,company_type) VALUES " +
                " ('"+companyName+"', '"+address+"', '"+ DateTimeKit.now() +"'," +
                " '"+ money +"', '"+setupTime+"','"+companyType+"'); ";
        sqlMapper.insert(sql2);
        return getOuterList();
    }

    //添加
    @RequestMapping(value = "/sharesAreCompanyAddOkRight", method = RequestMethod.POST)
    @ResponseBody
    public List<SharesAreCompanyInfo> sharesAreCompanyAddOkRight(String companyName, String companyType,String address,String money,String setupTime) {
        String sql2 = "INSERT INTO share_sare_company_info (company_name,address, createtime,money,setupTime,company_type) VALUES " +
                " ('"+companyName+"', '"+address+"', '"+ DateTimeKit.now() +"'," +
                " '"+ money +"', '"+setupTime+"','"+companyType+"'); ";
        sqlMapper.insert(sql2);
        return getBranchList();
    }

    //跳到编辑页面
    @RequestMapping(value = "/sharesAreCompanyEditLeft", method = RequestMethod.POST)
    @ResponseBody
    public SharesAreCompanyInfo sharesAreCompanyEditLeft(String id,Model model) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql ="select * from share_sare_company_info where id='"+id+"' ";
        SharesAreCompanyInfo info=sqlMapper.selectOne(sql,SharesAreCompanyInfo.class);
        return info;
    }

    //编辑
    @RequestMapping(value = "/sharesAreCompanyEditOkLeft", method = RequestMethod.POST)
    @ResponseBody
    public List<SharesAreCompanyInfo> sharesAreCompanyEditOkLeft(String companyName, String address,String id,String money,String setupTime) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "update share_sare_company_info set company_name='"+companyName+"', address='"+address+"', money='"+money+"', setupTime='"+setupTime+"'  " +
                " where id ='"+id+"' ";
        sqlMapper.update(sql);
        return getOuterList();
    }


    //跳到编辑页面
    @RequestMapping(value = "/sharesAreCompanyEditRight", method = RequestMethod.POST)
    @ResponseBody
    public SharesAreCompanyInfo sharesAreCompanyEditRight(String id,Model model) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql ="select * from share_sare_company_info where id='"+id+"' ";
        SharesAreCompanyInfo info=sqlMapper.selectOne(sql,SharesAreCompanyInfo.class);
        return info;
    }

    //编辑
    @RequestMapping(value = "/sharesAreCompanyEditOkRight", method = RequestMethod.POST)
    @ResponseBody
    public List<SharesAreCompanyInfo> sharesAreCompanyEditOkRight(String companyName, String address,String id,String money,String setupTime) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "update share_sare_company_info set company_name='"+companyName+"', address='"+address+"', money='"+money+"', setupTime='"+setupTime+"'  " +
                " where id ='"+id+"' ";
        sqlMapper.update(sql);
        return getBranchList();
    }

    //删除
    @RequestMapping(value = "/sharesAreCompanyDeleteLeft", method = RequestMethod.POST)
    @ResponseBody
    public List<SharesAreCompanyInfo> sharesAreCompanyDeleteLeft(String id) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "delete from share_sare_company_info where id ='"+id+"' ";
        sqlMapper.delete(sql);
        return getOuterList();
    }

    //删除
    @RequestMapping(value = "/sharesAreCompanyDeleteRight", method = RequestMethod.POST)
    @ResponseBody
    public List<SharesAreCompanyInfo> sharesAreCompanyDeleteRight(String id) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "delete from share_sare_company_info where id ='"+id+"' ";
        sqlMapper.delete(sql);
        return getBranchList();
    }

    //跳到分配股份页面
    @RequestMapping("/distributionSharesArePeople/{id}")
    public String distributionSharesArePeople(@PathVariable String id,Model model) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = " select * from share_sare_people_info ";
        List<SharesArePeopleInfo> peopleInfo=sqlMapper.selectList(sql,SharesArePeopleInfo.class);
        model.addAttribute("id",id);
        model.addAttribute("peopleInfo",peopleInfo);
        return PREFIX + "distributionSharesArePeople.html";
    }

    //加载公司对于股东
    @RequestMapping(value = "/getShareSareRelationInfo", method = RequestMethod.POST)
    @ResponseBody
    public List<SharesAreCompanyInfo> getShareSareRelationInfo(String id) throws Exception {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String sql = "select * from share_sare_relation_info a left join user b on a.userid=b.id" +
                " where share_sare_company_id ='"+id+"' ";
        List<SharesAreCompanyInfo> list =sqlMapper.selectList(sql,SharesAreCompanyInfo.class);
        return list;
    }

    @RequestMapping(value = "/shareholderSubmitLeftAdd")
    public String shareholderSubmitLeftAdd(Model model) {
        return PREFIX + "shareholderSubmitLeftAdd.html";
    }

}
